/**
 * 检查是否为纯数字
 * @param {String} data 
 */
const PureNumber = (data) => {
  return /^\d+$/.test(data);
};
/**
 * 字符
 * @param {*} data 
 * @returns 
 */
const PureLetter = (data) => {
  return /^[a-zA-Z]+$/.test(data);
};
/**
 * 数字字符
 * @param {*} data 
 * @returns 
 */
const PureNumberLetter = (data) => {
  return /^[0-9A-Za-z]+$/.test(data);
};
/**
 * 数字字符下划线
 * @param {*} data 
 * @returns 
 */
const PureNumberLetterUnderline = (data) => {
  return /^\w+$/.test(data);
};
/**
 * 6-16位字符，请使用字母加数字或下划线的组合，不能单独使用字母、数字或下划线。
 * @param {String} data 登录密码字符串
 * @returns 正则校验正确
 */
const LoginPassword = (data) => {
  return !PureNumber(data) && !PureLetter(data) && PureNumberLetterUnderline(data) && data.length <= 16 && data.length >= 6;
};
/**
 * 只支持整数和小数(保留两位小数)
 * @param {*} data   
 */
const DigitNumber = (data) => {
  return /^[0-9]*(\.[0-9]{1,2})?$/.test(data);
};
const NormalCarPlate = (data) => {
  return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/.test(data);
};
const NewPowerCarPlate = (data) => {
  return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF][A-HJ-NP-Z0-9][0-9]{4}))$/.test(data);
};

const AllCarPlate = (data) => {
  return NormalCarPlate(data) || NewPowerCarPlate(data);
};

function Email (data) {
  return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(data);
}

function Phone (data) {
  return /^[1]([3-9])[0-9]{9}$/.test(data);
}

function PhoneStrict (data) {
  return /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/.test(data);
}

function IdCard (data) {
  return /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(data);
}
/**
 * 字符串判空
 * @param {*} data 
 */
function Empty (data) {

  return data == undefined || data == null || typeof data != 'string' || data.length == 0;
}
/*** 
格式化小数
 *@params tp {Number or String} 数值 
*@params digit {Number} 小数点的位数，不够补0 
 *@returns {String} 格式化后的  数值
**/
function FormatDigit (data, digit) {
  if (digit == undefined || digit == null) digit = 2;
  var tp = '0.00';
  if (undefined != data) {
    tp = data;
  }
  tp = new Number(tp);
  if (isNaN(tp)) {
    return '0.00';
  }
  tp = tp.toFixed(digit) + '';
  var re = /^(-?\d+)(\d{3})(\.?\d*)/;
  while (re.test(tp)) {
    tp = tp.replace(re, '$1,$2$3');
  }

  return tp;
}
/**
 * 字符串参数序列
 * @param {字符串} source 
 * @param {参数列表} params 
 * @returns 
 */
function Format (source, params) {
  if (!params instanceof Array) {
    return source;
  }
  for (let i = 0; i < params.length; i++) {
    source = source.replace(new RegExp('\\{' + i + '\\}', 'g'), params[i]);
  }
  return source;
}
/**
 * 对象转参数字符串
 * @param {Object} obj 
 * @param {Boolean} encode 
 * @returns 
 */
function ObjToQuery (obj, encode, question) {
  let keys = Object.keys(obj)
  let query = ""
  for (let i = 0; i < keys.length; i++) {
    query += (i == 0 ? (question == false ? "" : "?") : "&") + keys[i] + "=" + (encode ? encodeURIComponent(obj[keys[i]]) : obj[keys[i]])
  }
  return query
}
/**
 * 参数字符串转对象
 * @param {String} query 参数字符串
 * @param {Boolean} decode 解密URL
 * @returns 
 */
function QueryToObj (query, decode) {
  let obj = {}
  if (Empty(query)) return obj
  if (query[0] == '?') query = query.substring(1)

  let vars = query.split("&");
  for (let i = 0; i < vars.length; i++) {
    let pair = vars[i].split("=");
    obj[pair[0]] = (decode ? decodeURIComponent(pair[1]) : pair[1])
  }
  return obj
}
function FileNameInfo (obj) {
  let info = {
    ext: "", name: "", full: ""
  }
  let _info = obj.match(new RegExp('[^/]+$', "ig"))
  if (_info == null) {
    return info
  } else {
    info.full = _info[0]
    let ex = _info[0].match(new RegExp('[^.]+', "ig"))
    if (ex != null && ex.length == 2) {
      info.ext = ex[1]
      info.name = ex[0]
    }

  }

  return info
}
/**
 * 隐藏字符串敏感信息
 * @param {String} str 字符串
 * @returns 隐藏敏感信息后的字符串
 */
function StrHidden (str, start_hold, end_hold, hidden_char) {
  if (start_hold == undefined || start_hold == null) start_hold = 3;
  if (end_hold == undefined || end_hold == null) end_hold = 4;
  if (hidden_char == undefined || hidden_char == null) hidden_char = '*';
  let str_hidden = str
  if (str.length > start_hold + end_hold) {
    let _str_start = str.slice(0, 3)
    let _str_end = str.slice(-4)
    str_hidden = _str_start + hidden_char.repeat(str.length - start_hold - end_hold) + _str_end
  }
  return str_hidden
}
export default {
  IdCard,
  Phone,
  PhoneStrict,
  Empty,
  Email,
  PureNumber,
  PureLetter,
  PureNumberLetter,
  PureNumberLetterUnderline,
  LoginPassword,
  DigitNumber,
  AllCarPlate,
  NewPowerCarPlate,
  NormalCarPlate,
  FormatDigit,
  Format,
  ObjToQuery,
  QueryToObj,
  StrHidden,
  FileNameInfo,
};